package at.tugraz.akm.forume;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import at.tugraz.akm.forume.extras.JSONUtils;
import at.tugraz.akm.forume.extras.MySharedPreferences;
import at.tugraz.akm.forume.webservice.Client;

public class NewUser extends Activity {

	EditText username;
	EditText email;
	EditText password;
	ProgressBar sending;
	LinearLayout main_content;
	TextView error_empty;
	TextView error_creating;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_user);
		username = (EditText) findViewById(R.id.username);
		email = (EditText) findViewById(R.id.email);
		password = (EditText) findViewById(R.id.password);
		sending = (ProgressBar) findViewById(R.id.sending);
		main_content = (LinearLayout) findViewById(R.id.ll_content);
		error_empty = (TextView) findViewById(R.id.error_empty);
		error_creating = (TextView) findViewById(R.id.error_creating);

		final Button create = (Button) findViewById(R.id.create);
		create.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!username.getText().toString().isEmpty()
						&& !email.getText().toString().isEmpty()
						&& !password.getText().toString().isEmpty()) {
					sending.setVisibility(View.VISIBLE);
					main_content.setVisibility(View.GONE);
					create.setVisibility(View.GONE);
					new SendTask().execute();
				} else {
					error_empty.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	public class SendTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				;
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void voids) {
			if (Client.newUser(getApplicationContext(), username.getText()
					.toString().trim(), email.getText().toString().trim(),
					password.getText().toString().trim())) {
				Toast.makeText(getApplicationContext(),
						getString(R.string.created_thread_successfully),
						Toast.LENGTH_SHORT);
				MySharedPreferences.setString(getApplicationContext(),
						MySharedPreferences.USERNAME, username.getText()
								.toString());
				MySharedPreferences.setString(getApplicationContext(),
						MySharedPreferences.PASSWORD, password.getText()
								.toString());
				String user_id = JSONUtils.getString(
						"http://nibble.li/forume/user.php?action=user_id&username="
								+ username.getText().toString(), "user_id");
				MySharedPreferences.setString(getApplicationContext(),
						MySharedPreferences.USER_ID, user_id);
				startActivity(new Intent(NewUser.this, Main.class)
						.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			} else {
				Log.e("Creado", "Buuuuu");
			}
		}
	}
}
